//
//  YMStatusViewModel.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/23.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

class YMStatusViewModel: NSObject {

    //定义一个模型属性
    var status:YMStatus?
    //微博认证用户图片
    var avatarImage:UIImage?
    //处理会员图片
    var memberImage:UIImage?
    //微博来源
    var soucreAttr:NSMutableAttributedString?
    
    // 转发数
    var repostsCount:String?
    // 评论数
    var commentsCount:String?
    // 点赞数
    var attitudesCount:String?
    //时间
    var timeStr:String?{
        //sinaDescripitionStr
        //status?.created_at
        return NSDate.sinaDate(status?.created_at ?? "")?.sinaDescripitionStr
    }
    //构造函数
    
    init(status:YMStatus) {
        super.init()
        //暂时不实现 需要定义两个方法赋值
        self.status = status
        self.repostsCount = getSinaCount(status.reposts_count, title: "转发")
        self.commentsCount = getSinaCount(status.comments_count, title: "评论")
        self.attitudesCount = getSinaCount(status.attitudes_count, title: "赞")
        self.avatarImage = getAvatarImage(status.user?.verified)
        self.memberImage = GetMemberImage(status.user?.mbrank)
        self.soucreAttr = getSoucreStr(status.source)
    }
    
    
    
}
//        MARK: - 处理转发评论赞的业务逻辑
extension YMStatusViewModel{
    func getSinaCount(sina:Int ,title:String) ->String{
        
        if sina <= 0{
           return title
        }else{
            if sina < 10000{
                return "\(sina)"
            }else{
                
                let sinaCount = CGFloat(sina/1000)/10
                var sinaCountStr = "\(sinaCount)万"

                if sinaCountStr.containsString(".0"){
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                    
                }
                return sinaCountStr
            }
            
            
        }
    }
}



extension YMStatusViewModel{
    //处理认证用户图片1.没认证 2.3.5企业认证 220.达人认证
    
    func getAvatarImage(verified:Int?) ->UIImage?{
        guard let ver = verified else{
            return nil
        }
        
        switch ver {
        case 1:
            return UIImage( named: "avatar_vip")
        case 2,3,5:
            return UIImage( named: "avatar_enterprise_vip")
        case 220:
            return UIImage( named: "avatar_grassroot")
        default:
            return UIImage( named: "avatar_vgirl")
        }
    }
    
    //处理会员图片 根据等级
    
    func GetMemberImage(mbrank:Int?) ->UIImage? {
        guard let mbr = mbrank else{
            return nil
        }
        if mbr > 0 && mbr < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mbr)")
        }
        return UIImage(named: "common_icon_membership_level")
    }
    
    
    
    
}

//        MARK: - 微博来源
extension YMStatusViewModel{
    
    func getSoucreStr(soucre:String?) ->NSMutableAttributedString{
        
        guard let  s = soucre where s.containsString("\">") else{
            
            let str = "上海三期微博"
            return getSoucreAttr(str)
        }
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSoucreAttr(result)
    }
    
    
    func getSoucreAttr(result:String) ->NSMutableAttributedString{
        let name = "来自 " + result
        let range = (name as NSString).rangeOfString("来自")
        let attr = NSMutableAttributedString(string: name)
        attr.addAttributes([NSForegroundColorAttributeName:UIColor.grayColor()], range: range)
        return attr
    }
    
    
    
    
}


